package com.demo.ping.service.impl

import com.demo.ping.dao.ErrorLogsRepository
import com.demo.ping.orm.entity.ErrorLogs
import com.demo.ping.orm.enumeration.ErrorLevelEnum
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.mockito.InjectMocks
import org.mockito.Mock
import org.mockito.junit.jupiter.MockitoExtension
import org.springframework.http.HttpStatus

import static org.junit.jupiter.api.Assertions.assertThrows
import static org.mockito.ArgumentMatchers.any
import static org.mockito.Mockito.never
import static org.mockito.Mockito.verify

/**
 * @author Jofo
 */
@ExtendWith(MockitoExtension)
class ErrorLogsServiceImplTest {

    @Mock
    ErrorLogsRepository errorLogsRepository

    @InjectMocks
    ErrorLogsServiceImpl errorLogsService


    @Test
    void saveErrorLog_NullErrorCode_ThrowsIllegalArgumentException() {
        def errorLogs = new ErrorLogs()
        errorLogs.errorMessage = "error message"
        errorLogs.errorLevel = ErrorLevelEnum.MEDIUM.getMessage()

        def exception = assertThrows(IllegalArgumentException) {
            errorLogsService.saveErrorLog(errorLogs)
        }

        assert exception.message == "errorCode is required"
        verify(errorLogsRepository, never()).save(any(ErrorLogs))
    }

    @Test
    void saveErrorLog_NullErrorMessage_ThrowsIllegalArgumentException() {
        def errorLogs = new ErrorLogs()
        errorLogs.errorCode = HttpStatus.TOO_MANY_REQUESTS.value()
        errorLogs.errorLevel = "ERROR"

        def exception = assertThrows(IllegalArgumentException) {
            errorLogsService.saveErrorLog(errorLogs)
        }

        assert exception.message == "errorMessage is required"
        verify(errorLogsRepository, never()).save(any(ErrorLogs))
    }

    @Test
    void saveErrorLog_NullErrorLevel_ThrowsIllegalArgumentException() {
        def errorLogs = new ErrorLogs()
        errorLogs.errorCode = HttpStatus.TOO_MANY_REQUESTS.value()
        errorLogs.errorMessage = "error message"

        def exception = assertThrows(IllegalArgumentException) {
            errorLogsService.saveErrorLog(errorLogs)
        }

        assert exception.message == "errorLevel is required"
        verify(errorLogsRepository, never()).save(any(ErrorLogs))
    }

    @Test
    void saveErrorLog_ValidData_SuccessfulSave() {
        def errorLogs = new ErrorLogs()
        errorLogs.errorCode = HttpStatus.TOO_MANY_REQUESTS.value()
        errorLogs.errorMessage = "error message"
        errorLogs.errorLevel = "ERROR"

        errorLogsService.saveErrorLog(errorLogs)

        verify(errorLogsRepository).save(any(ErrorLogs))
    }
}
